#include <SimpleDef.h>
/* 逆置字符串 */
static void reverse(char* pString, int length)
{
    char* pStrEnd = pString + length - 1; /* 末尾 */
    char temp;
    while (pString < pStrEnd)
    {
        temp = *pString;
        *pString = *pStrEnd;
        *pStrEnd = temp;
        pString ++;
        pStrEnd --;
    }
}
char* itoa(int value, char* pStr, int radix)
{
    if (value == 0)
    {
        pStr[0] = '0';
        pStr[1] = '\0';
        return pStr;
    }
    if (radix <=0 || radix > 36) /* error */
    {
        pStr[0] = '\0';
        return pStr;
    }
    bool isNeg = false;
    unsigned int abs = (unsigned int) value; /* 负数补码表示，正数原码表示 */
    if (value < 0 && radix == 10) /* 十进制才考虑负号 */
    {
        isNeg = true;
        abs = ~abs + 1; /* 诸位取反再加一，得到绝对值 */
    }
    int i = 0;
    pStr[i++] = '\0';
    while (abs != 0)
    {
        int remainder = abs % radix;
        abs /= radix;
        pStr[i++] = (remainder > 9) ? (remainder - 10) + 'A' : remainder + '0';
    }
    if (isNeg) pStr[i++] = '-';
    reverse(pStr, i);
    return pStr;
}
